import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";

export const routes = [
  { path: "/", redirect: "/home", hidden: true },

  {
    path: "/403",
    name: "403",
    hidden: true,
    lable: "错误",
    component: () => import("@/views/layout/error.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    name: "login",
    hidden: true,
    lable: "登录",
  },
  {
    path: "/personal",
    component: () => import("@/views/personal/index.vue"),
    name: "personal",
    hidden: true,
    lable: "个人中心",
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/layout/home.vue"),
    lable: "首页",
    icon: "House",
  },
  {
    lable: "轮播图管理",
    name: "banner",
    icon: "Tickets",
    path: "/banner",
    component: () => import("@/views/layout/index.vue"),
    children: [
      {
        lable: "轮播图列表",
        path: "list",
        name: "banner-list",
        component: () => import("@/views/banner/list.vue"),
      },
      {
        lable: "轮播图新增",
        path: "add",
        name: "banner-add",
        component: () => import("@/views/banner/add.vue"),
      },
    ],
  },
  {
    path: "/manager",
    name: "manager",
    lable: "管理员管理",
    icon: "Monitor",
    component: () => import("@/views/layout/index.vue"),
    children: [
      {
        lable: "管理员列表",
        path: "list",
        name: "manager-list",
        component: () => import("@/views/manager/list.vue"),
        // children: [
        //   {
        //     lable: "管理员列表-1",
        //     name: "manager-list-1",
        //     path: "list",
        //     component: () => import("@/views/manager/list.vue"),
        //   },
        //   {
        //     lable: "管理员列表-2",
        //     name: "manager-list-2",
        //     path: "list",
        //     component: () => import("@/views/manager/list.vue"),
        //   },
        // ],
      },
    ],
  },
  {
    path: "/users",
    name: "users",
    lable: "用户管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "User",
    children: [
      {
        path: "list",
        name: "users-list",
        lable: "用户列表",
        component: () => import("@/views/users/list.vue"),
      },
    ],
  },
  {
    path: "/pro",
    name: "pro",
    lable: "商品管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "Goods",
    children: [
      {
        lable: "商品列表",
        path: "list",
        name: "pro-list",
        component: () => import("@/views/pro/list.vue"),
      },
      {
        lable: "秒杀列表",
        path: "seckill",
        name: "seckill",
        component: () => import("@/views/pro/seckill.vue"),
      },
      {
        lable: "推荐列表",
        path: "recommendl",
        name: "recommend",
        component: () => import("@/views/pro/recommend.vue"),
      },
      {
        lable: "筛选列表",
        path: "screen",
        name: "screen",
        component: () => import("@/views/pro/screen.vue"),
      },
    ],
  },
  {
    path: "/cart",
    name: "cart",
    lable: "购物车管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "ShoppingCart",
    children: [
      {
        path: "list",
        name: "cart-list",
        lable: "购物车列表",
        component: () => import("@/views/cart/list.vue"),
      },
    ],
  },
  {
    path: "/order",
    name: "order",
    lable: "订单管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "Tickets",
    children: [
      {
        path: "list",
        name: "order-list",
        lable: "订单列表",
        component: () => import("@/views/order/order.vue"),
      },
    ],
  },
  {
    path: "/echarts",
    name: "echarts",
    lable: "数据可视化",
    component: () => import("@/views/layout/index.vue"),
    icon: "View",
    children: [
      {
        lable: "echarts使用",
        path: "list",
        name: "echarts-list",
        component: () => import("@/views/echarts/list.vue"),
      },
    ],
  },
  {
    path: "/tiny",
    name: "tiny",
    lable: "编辑器",
    component: () => import("@/views/layout/index.vue"),
    icon: "Edit",
    children: [
      {
        lable: "富文本编辑器",
        path: "texteditor",
        name: "texteditor",
        component: () => import("@/views/tiny/texteditor.vue"),
      },
    ],
  },
  {
    path: "/excel",
    name: "excel",
    lable: "excel管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "Folder",
    children: [
      {
        lable: "excel导入",
        path: "in",
        name: "excelin",
        component: () => import("@/views/excel/excelin.vue"),
      },
      {
        lable: "excel导出",
        path: "out",
        name: "excelout",
        component: () => import("@/views/excel/excelout.vue"),
      },
    ],
  },
  {
    path: "/map",
    name: "map",
    lable: "地图管理",
    component: () => import("@/views/layout/index.vue"),
    icon: "Place",
    children: [
      {
        lable: "百度地图",
        path: "baidu",
        name: "mapbaidu",
        component: () => import("@/views/map/mapbaidu.vue"),
      },
    ],
  },
];

//静态路由  不需要访问权限
export const Staticroute = [
  {
    path: "/403",
    name: "403",
    hidden: true,
    lable: "错误",
    component: () => import("@/views/layout/error.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    name: "login",
    hidden: true,
    lable: "登录",
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

//不用跳转登入页面的路径后缀
const whitename = ["login", "403", "personal", "home"];
router.beforeEach((to, from, next) => {
  // console.log(to);
  if (whitename.includes(to.name)) {
    next();
    //当本地存储有token说明已经登入  则不需要跳转登入页面
  } else if (localStorage.getItem("token")) {
    if (store.state.role === 2 || store.state.jurisdiction.includes(to.name)) {
      next();
    } else {
      next("/403");
    }
  } else {
    next("/login");
  }
});

export default router;
